﻿using B.S.DataBase.MyProject.Domain.基础设置;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Write.API.Appcation.Commands.BasicSettings;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.BasicSettings
{
    /// <summary>
    /// 种病管理-删除命令处理
    /// </summary>
    public class DeleteDiseaseManagementCommandHandler : IRequestHandler<DeleteDiseaseManagementCommand, APIResult<int>>
    {
        /// <summary>
        /// SqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sqlSugar">SqlSugar</param>
        public DeleteDiseaseManagementCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<APIResult<int>> Handle(DeleteDiseaseManagementCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();
                var list = sqlSugar.db.Queryable<DiseaseManagement>().Where(x => x.DId == request.DId).First();
                list.IsDel = true;
                var res = sqlSugar.db.Updateable<DiseaseManagement>().ExecuteCommand();
                if (res > 0)
                {
                    result.Code = APIEnum.删除成功;
                    result.Msg = APIEnum.删除成功.ToString();
                    result.data = res;
                }
                else
                {
                    result.Code = APIEnum.删除失败;
                    result.Msg = APIEnum.删除失败.ToString();
                    result.data = res;
                }
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
